package com.payment.service;

import com.payment.mapper.OrderMapper;
import com.payment.model.UserInfo;
import com.payment.model.UserOrderInfo;
import com.payment.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class OrderService {
    @Autowired
    OrderMapper orderMapper;

    /**
     * 根据当前时间，用户信息，生成一串不重复的订单编号，用于付款
     * */
    public String randomOrderCode(){
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String formattedDate = sdf.format(currentDate);
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        String orderCode = formattedDate+uuid.substring(0,7);
        return orderCode;
    }

    /**
     * 通过生成随机订单编号，添加用户收付款临时表，
     * @参数 使用对象，包含user_id和order_code
     * */
    public Response addUserOrderCode(UserInfo userInfo){
        String order_code = randomOrderCode();
        userInfo.setOrder_code(order_code);
        if(orderMapper.addUserOrderCode(userInfo)>0){
            //添加成功
            return new Response(200,"生成和添加成功",order_code);
        }else{
            return new Response(203,"生成和添加失败",null);
        }
    }

    /**
     * 第一步验证用户付款码是否存在，
     * 第二步验证用户付款码是否过期
     * 第三步才执行用户扣款操作
     * 3.1 判断用户的余额是否足够
     * 3.2 余额足够则执行扣款操作：1.修改用户表的余额 2.添加付款订单信息
     * */
    public Response payment(UserInfo userInfo){
        //获取付款人的信息
        UserOrderInfo userOrderInfo = orderMapper.validateOrderCode(userInfo);
        if(userOrderInfo!=null){
            //判断当前用户和付款用户不是同一个
            if(userInfo.getUser_id()==userOrderInfo.getUser_id()){
                return new Response(203,"不能给自己付款",null);
            }
            //当前付款码可用
            //取出收款用户的id
            Integer current_user_id = userInfo.getUser_id();
            //收款人
            UserInfo payee = new UserInfo();
            payee.setUser_id(current_user_id);
            //设置付款人的id
            UserInfo payer = new UserInfo();
            payer.setUser_id(userOrderInfo.getUser_id());
            //获取付款人的余额
            String payer_balance = orderMapper.getBalanceByUserId(payer).getBalance();
            //获取收款人的余额
            String payee_balance = orderMapper.getBalanceByUserId(payee).getBalance();
            String order_pay = userInfo.getOrder_pay();
            if(validataBalance(order_pay,payer_balance)){
                //付款人有余额，执行扣除操作
                payer.setBalance(subCutBalance(order_pay,payer_balance));
                //收款人 增加余额
                payee.setBalance(addBalance(order_pay,payee_balance));
                orderMapper.updateUserBalanceByUserId(payee);
                //扣除当前用户的余额，并添加付款订单记录
                if(orderMapper.updateUserBalanceByUserId(payer)>0){
                    //修改余额成功
                    //补充用户对象中收款人和付款人id
                    userInfo.setPayee_id(current_user_id);
                    userInfo.setPayer_id(userOrderInfo.getUser_id());
                    System.out.println("添加订单");
                    System.out.println(userInfo);
                    try{
                        if(orderMapper.addPayOrder(userInfo)>0){
                            //删除付款码
                            orderMapper.deleteUserOrderCodeByUserId(payer);
                            //添加付款订单信息成功
                            return new Response(200,"付款成功",null);
                        }else{
                            return new Response(203,"付款订单信息添加失败",null);
                        }
                    }catch (Exception e){
                        System.out.println(e);
                        return new Response(203,"付款订单信息添加失败",null);
                    }
                }else{
                    return new Response(203,"付款失败",null);
                }
            }else {
                return new Response(203,"余额不足",null);
            }
        }else {
            return new Response(203,"当前付款码已过期",null);
        }
    }

    /**
     * 封装类型转换方法
     * String to double
     * */
    public boolean validataBalance(String order_pay,String balance){
        double pay = Double.parseDouble(order_pay);
        double balan =Double.parseDouble(balance);
        if((balan-pay)>=0){
            //有余额
            return true;
        }else{
            //没有余额
            return false;
        }
    }

    /**
     * 计算剩余
     *
     * */
    public String subCutBalance(String order_pay,String balance){
        double pay = Double.parseDouble(order_pay);
        double balan =Double.parseDouble(balance);
        return Double.toString(balan - pay);
    }

    /**
     * 计算余额 收款
     * */
    public String addBalance(String order_pay,String balance){
        double pay = Double.parseDouble(order_pay);
        double balan =Double.parseDouble(balance);
        return Double.toString(balan + pay);
    }
}
